package View;

sub new
{
	my ($class)		= @_;		
	my $self		= {};

	$self->{view_id}	= '';
	$self->{recipient_key}	= '';
	$self->{client_ip}	= '';
	$self->{campaign_id}	= 0;
	$self->{recipient_id}	= '';

	# 	-- commented because we'd like to use mysql default (CURRENT_TIMESTAMP) if nothing passed in
	# $self->{server_time}	= '';
	$self->{http_referrer}	= '';
	$self->{http_user_agent}= '';

	bless $self, 'View';
	return $self;
}

sub newFromDB
{
	my ($class, $view_id)	= @_;
	my $self		= {};

	my $stmt	= "SELECT id, url, recipient_key, ip, server_time, http_referrer, http_user_agent, campaign_id
			   FROM views WHERE id = $view_id";
	
	my $hashref	= $Utils::kit_db->GetSingleRow($stmt);

	$self->{view_id}	= $view_id;
	$self->{recipient_key}	= $hashref->{recipient_key};
	$self->{client_ip}	= $hashref->{ip};
	$self->{server_time}	= $hashref->{server_time};
	$self->{http_referrer}	= $hashref->{http_referrer};
	$self->{http_user_agent}= $hashref->{http_user_agent};
	$self->{campaign_id}	= $hashref->{campaign_id};

	bless $self, 'View';
	return $self;
}

sub InsertView
{
	my ($self)	= @_;
	my $stmt	= "INSERT INTO views (recipient_key, ip, http_referrer, http_user_agent, campaign_id)
			   VALUES (\"$self->{recipient_key}\", \"$self->{client_ip}\",
			   	   \"$self->{http_referrer}\", \"$self->{http_user_agent}\", $self->{campaign_id})";

# 	print $stmt; exit;
	return $Utils::kit_db->Insert($stmt);
}

sub GetCampaignViews
{
	my ($self, $campaign_id)	= @_;

	my $stmt	= "SELECT id, count(recipient_key) as views_count, recipient_key, ip, server_time, http_referrer, http_user_agent
			   FROM   views 
			   WHERE  campaign_id = $campaign_id
			   GROUP BY recipient_key";
        # print $stmt . "<p>";
	my @rows	= $Utils::kit_db->GetMultipleRows($stmt);
	my @views	= ();

	foreach (@rows) 
	{
		my $view_id	= $_->[0];
		my $views_count	= $_->[1];
		my $ip		= $_->[3];
		my $server_time	= $_->[4];
		my $http_referrer	= $_->[5];
		my $http_user_agent	= $_->[6];

                # -- incase recipient key is empty
                my $recipient, $rcpt_name, $rcpt_id, $company_name_short;
                if ($_->[2])
                {
                    $recipient		= newFromDBByKey Recipient($_->[2]);
                    $rcpt_name		= $recipient->{f_name} . ' ' . $recipient->{l_name};
                    $rcpt_id            = $recipient->{recipient_id};
                    $company_name_short	= $recipient->{company_name_short};
                }

		my %view	= (
			view_id		=> $view_id,
			recipient_name	=> $rcpt_name,
			recipient_id    => $rcpt_id,
			ip		=> $ip,
			server_time	=> $server_time,
			http_referrer	=> $http_referrer,
			http_referrer_substr	=> substr($http_referrer, 0, 32),
			http_user_agent	=> $http_user_agent,
			company_name_short => $company_name_short,
			email		=> $recipient->{email_addr},
			views_count	=> $views_count,
		);
		push @views, \%view;
	}
	return @views;
}

sub get_views_by_user
{
    my $self    = shift;

    my $stmt    = "SELECT   V.campaign_id 
                   FROM     recipients AS R, views AS V 
                   WHERE    R.random_key = V.recipient_key 
                   AND      R.id = $self->{recipient_id};";

    my @campaign_ids    = $Utils::kit_db->GetMultipleRows($stmt);

# printf "%s<p>ary count = %d<br>", $stmt, $#campaign_ids;
    my @campaigns   = ();

    foreach (@campaign_ids) 
    {
        my $campaign    = newFromDB Campaign($_->[0]);
        my $group       = newFromDB Groups($campaign->{group_id});
        my $content     = newFromDB Content($campaign->{content_id});

# printf "c_id = %s <br>group = %s <br>c_content = %s", $campaign->{campaign_id}, $group->{group_name}, $content->{title};

        my %campaign    = (
            campaign_id         => $campaign->{campaign_id},
            campaign_group      => $group->{group_name},
            campaign_content    => $content->{title},
            view_count          => 
        );

        push @campaigns, \%campaign;
    }
#     print "camps = " . @campaigns;
    return @campaigns;
}
1;